/**
 * Copyright 2014,Peak Tai 台俊峰(taijunfeng_it@sina.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.cenluan.vote;

import java.util.ArrayList;
import java.util.List;

import com.cenluan.kit.XssKit;
import com.cenluan.user.User;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;

/**
 * 投票，投票和选项两张表增加了冗余字段为了查询方便，投票时一次性存储，后面不需要维护
 * 
 * @author peak
 * 
 */
public class Vote extends Model<Vote> {

	private static final long serialVersionUID = 1L;

	public static final Vote dao = new Vote();
	/**
	 * 单选类型
	 */
	public static final String TYPE_SINGLE = "SINGLE";
	/**
	 * 多选类型
	 */
	public static final String TYPE_MULTIPLE = "MULTIPLE";
	/**
	 * 活跃的，有效的状态
	 */
	public static final String STATE_ACTIVE = "ACTIVE";
	/**
	 * 已删除的
	 */
	public static final String STATE_DELETED = "DELETED";

	/**
	 * 获取选项
	 * 
	 * @return
	 */
	public List<VoteOption> getOptions() {
		return VoteOption.dao.find("select * from t_vote_option "
				+ "where voteId=? order by id asc", getStr("id"));
	}

	/**
	 * 获取创建者
	 * 
	 * @return
	 */
	public User getCreater() {
		return User.dao.findById(getStr("createrId"));
	}

	/**
	 * 判断是否投过票
	 * 
	 * @param userId
	 * @return
	 */
	public boolean hasVoted(String userId) {
		StringBuilder sql = new StringBuilder();
		sql.append(" select count(*) ");
		sql.append(" from t_vote_option vo ");
		sql.append(" left join t_vote_option_user vou ");
		sql.append(" on vou.optionId=vo.id ");
		sql.append(" where vo.voteId=? and vou.userId=? ");
		return Db.queryLong(sql.toString(), getStr("id"), userId) > 0;
	}

	/**
	 * 增加总票数
	 * 
	 * @param count
	 * @return
	 */
	public boolean addTotalVotes(int count) {
		return Db.update("update t_vote set totalVotes=totalVotes+?"
				+ " where id=?", count, getStr("id")) == 1;
	}

	/**
	 * 增加总投票人数
	 */
	public boolean addTotalUsers() {
		return Db.update("update t_vote set totalUsers=totalUsers+1"
				+ " where id=?", getStr("id")) == 1;
	}

	/**
	 * 返回一个html代码字符串，是个访问该投票的地址
	 * 
	 * @return
	 */
	public String getHtml() {
		return "<a href='/vote/show/" + getStr("id") + "'>"
				+ XssKit.escapeStr(getStr("title")) + "</a>";
	}

	/**
	 * 分页查询投票
	 * 
	 * @param pn
	 * @param pz
	 * @param order
	 *            排序:new,hot
	 * @param createrId
	 * @return
	 */
	public Page<Vote> page(int pn, int pz, String order, String createrId) {
		StringBuilder sql = new StringBuilder();
		List<Object> paras = new ArrayList<Object>();
		sql.append(" from t_vote ");
		sql.append(" where state=? ");
		paras.add(STATE_ACTIVE);
		if (StrKit.notBlank(createrId)) {
			sql.append(" and createrId=? ");
			paras.add(createrId);
		}
		if ("hot".equals(order)) {
			sql.append(" order by totalUsers desc,createTime desc ");
		} else {
			sql.append(" order by createTime desc ");
		}

		return paginate(pn, pz, "select *", sql.toString(), paras.toArray());

	}

}
